<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/5/14
 * Time: 14:25
 */

namespace app\commi\controller;

use think\Db;
use think\Request;


class Images extends Base
{
    /**
     * 赛事图片列表
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $mi_name = trim(input('mi_name'), ' ');
            $mi_paishe_name = trim(input('mi_paishe_name'), ' ');
            $where['mi_committee_id'] = session('com_id');
            if(isset($mi_name) && $mi_name != ''){
                $where['mi_name'] = ['like', '%' . $mi_name . '%'];
            }
            if(isset($mi_paishe_name) && $mi_paishe_name != ''){
                $where['mi_paishe_name'] = ['like', '%' . $mi_paishe_name . '%'];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('match_images')
                ->join('up_match', 'match_id = mi_match_id', 'left')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('match_images')
                ->join('up_match', 'match_id = mi_match_id', 'left')
                ->field('mi_id, mi_url, mi_name, mi_paishe_name, mi_paishe_time, mi_price, mi_create_time, 
                match_title')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('mi_create_time desc')
                ->select();
            if($lists){
                foreach($lists as $k => $v){
                    $lists[$k]['mi_paishe_time'] = date('Y-m-d H:i', strtotime($v['mi_paishe_time']));
                }
            }
            
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 上传赛事照片
     * @return mixed|\think\response\Json
     */
    public function imgAdd(){
        if(Request::instance()->isPost()){
            $mi_name = trim(input('mi_name'), ' ');
            $mi_url = input('mi_url');
            $mi_price = trim(input('mi_price'), ' ');
            $mi_paishe_time = input('mi_paishe_time');
            $mi_paishe_name = trim(input('mi_paishe_name'), ' ');
            $match = Db::table('up_match')
                ->where('match_committee_id', session('com_id'))
                ->where('match_member_id', session('cm_id'))
                ->field('match_id, match_cate')
                ->find();
            if(!$match){
                return json(['code' => 100, 'msg' => '请先创建赛事']);
            }
            $insert = [
                'mi_match_id' => $match['match_id'],
                'mi_committee_id' => session('com_id'),
                'mi_cate' => $match['match_cate'],
                'mi_url' => $mi_url,
                'mi_name' => $mi_name,
                'mi_paishe_name' => $mi_paishe_name,
                'mi_paishe_time' => $mi_paishe_time,
                'mi_price' => $mi_price,
                'mi_create_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_match_images')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '上传成功']);
            }else{
                return json(['code' => 100, 'msg' => '上传失败']);
            }
        }
        $lose = $this->lostTime();
        if($lose == 1){
            return $this->fetch('/lose');
        }else{
            return $this->fetch('images/imgadd');
        }
        
    }
    
    /**
     * 编辑赛事照片
     * @return mixed|\think\response\Json
     */
    public function imgEdit(){
        $mi_id = input('mi_id');
        if(Request::instance()->isPost()){
            $mi_name = trim(input('mi_name'), ' ');
            $mi_url = input('mi_url');
            $mi_price = trim(input('mi_price'), ' ');
            $mi_paishe_time = input('mi_paishe_time');
            $mi_paishe_name = trim(input('mi_paishe_name'), ' ');
            $update = [
                'mi_name' => $mi_name,
                'mi_paishe_name' => $mi_paishe_name,
                'mi_paishe_time' => $mi_paishe_time,
                'mi_price' => $mi_price
            ];
            if(!empty($mi_url)){
                $update['mi_url'] = $mi_url;
            }
            $res = Db::table('up_match_images')->where('mi_id', $mi_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $img = Db::table('up_match_images')
            ->where('mi_id', $mi_id)
            ->field('mi_url, mi_name, mi_paishe_name, mi_paishe_time, mi_price')
            ->find();
        $this->assign([
            'matchImg' => $img,
            'mi_id' => $mi_id
        ]);
        $lose = $this->lostTime();
        if($lose == 1){
            return $this->fetch('/lose');
        }else{
            return $this->fetch('images/imgedit');
        }
        
    }
    
    /**
     * 删除照片
     * @return \think\response\Json
     */
    public function imgDel(){
        $lose = $this->lostTime();
        if($lose == 1){
            return json(['code' => 100, 'msg' => '账号已过期，非法操作']);
        }
        $mi_id = input('id');
        $res = Db::table('up_match_images')->delete($mi_id);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}